package Test;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;

import java.io.IOException;

public class B1 {

    //提交的地址
    private static final String Url = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";
    //短语验证地址
    public Integer send(String tel){
        HttpClient client = new HttpClient();
        PostMethod method = new PostMethod(Url);

        client.getParams().setContentCharset("UTF-8");
        method.setRequestHeader("ContentType", "application/x-www-form-urlencoded;charset=UTF-8");

        //随机验证码
        int mobile_code = (int) ((Math.random() * 9 + 1) * 100000);

        String content = "您的验证码是：" + mobile_code + "。请不要把验证码泄露给其他人。";
        NameValuePair[] data = {//提交短信
                new NameValuePair("account", "sms-yx5618780"), //查看用户名是登录用户中心->验证码短信->产品总览->APIID
                new NameValuePair("password", "kZnw4yKiY475700"),  //查看密码请登录用户中心->验证码短信->产品总览->APIKEY
                //new NameValuePair("password", util.StringUtil.MD5Encode("密码")),
                new NameValuePair("mobile", tel),//接收验证码的手机
                new NameValuePair("content", content),//官方默认模板，个人不支持更改
        };
        method.setRequestBody(data);
        try {
            client.executeMethod(method);
            String SubmitResult = method.getResponseBodyAsString();
            //System.out.println(SubmitResult);
            Document doc = DocumentHelper.parseText(SubmitResult);
            //响应码如果为2，则发送成功
            if ("2".equals(doc.getRootElement().elementText("code"))) {
                return mobile_code;
            }
        } catch (IOException | DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //这是我自己定义的响应码，为发送失败，可能是号码有误或者短信服务欠费
        return 500;
    }
    public static void main(String[] args) {
        B1 o=new B1();
        o.send("17590813665");
    }

}
